/*
 * Copyright (c) 2009 Johnny Mast
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#import "Resources.h"


@implementation Resources
@synthesize activeblinkers;
@synthesize activespriteSheetTextures;
@synthesize currentFramesPerSecond;

static Resources *ResourcesObject;

+(Resources*)SharedResourceManager {
	@synchronized(self)
	{
		if (ResourcesObject == nil)
		{
			ResourcesObject = [[self alloc] init];
		}
	}
	return ResourcesObject;
}


- (void)dealloc {
    
    // Release the cachedTextures dictionary.
	[_cachedTextures release];
	[super dealloc];
}


- (id)init {
	// Initialize a dictionary with an initial size to allocate some memory, but it will 
    // increase in size as necessary as it is mutable.
	_cachedTextures = [[NSMutableDictionary dictionaryWithCapacity:10] retain];
	return self;
}


-(UIInterfaceOrientation)getCurrentDeviceOriontation {
	return [[UIDevice currentDevice] orientation];  
}

-(void)increaseActiveblinkers {
	activeblinkers ++;	
}

-(void)increaseActiveSpriteSheetTextures {
	activespriteSheetTextures ++;	
}

-(void)setTextureImageWithName:(NSString*)Texturename textureImage:(UIImage*)textureImage
{
    Texture2D *_cachedTexture = [[Texture2D alloc] initWithImage:textureImage filter:GL_NEAREST];
    [_cachedTextures setObject:_cachedTexture forKey:Texturename];
}


- (Texture2D*)getTextureWithName:(NSString*)aTextureName {
    
    // Try to get a texture from cachedTextures with the supplied key.
    Texture2D *_cachedTexture;
    
	if(DEBUG) NSLog(@"aTextureName == %@", aTextureName); 
	
    // If we can find a texture with the supplied key then return it.
    if(_cachedTexture = [_cachedTextures objectForKey:aTextureName]) {
        if(DEBUG) NSLog(@"INFO - Resource Manager: A cached texture was found with the key '%@'.", aTextureName);
        return _cachedTexture;
    }
    
    // As no texture was found we create a new one, cache it and return it.
	//  if(DEBUG) NSLog(@"INFO - Resource Manager: A texture with the key '%@' could not be found so creating it.", aTextureName);
    _cachedTexture = [[Texture2D alloc] initWithImage:[UIImage imageNamed:aTextureName] filter:GL_NEAREST];
    [_cachedTextures setObject:_cachedTexture forKey:aTextureName];
    
    // Return the texture which is autoreleased as the caller is responsible for it
    return [_cachedTexture autorelease];
}

- (BOOL)hasTextureWithName:(NSString*)name {
	if([_cachedTextures objectForKey:name])
		return YES;
	return NO;
}

- (BOOL)releaseTextureWithName:(NSString*)aTextureName {
	
    // Try to get a texture from cachedTextures with the supplied key.
    Texture2D *cachedTexture = [_cachedTextures objectForKey:aTextureName];
	
    // If a texture was found we can remove it from the cachedTextures and return YES.
    if(cachedTexture) {
        if(DEBUG) NSLog(@"INFO - Resource Manager: A cached texture with the key '%@' was released.", aTextureName);
		Texture2D* object = [_cachedTextures objectForKey:aTextureName];
        [_cachedTextures removeObjectForKey:aTextureName];
		[object release];
        return YES;
    }
    
    // No texture was found with the supplied key so log that and return NO;
    if(DEBUG) NSLog(@"INFO - Resource Manager: A texture with the key '%@' could not be found to release.", aTextureName);
    return NO;
}

- (void)releaseAllTextures {
    if(DEBUG) NSLog(@"INFO - Resource Manager: Releasing all cached textures.");
    [_cachedTextures removeAllObjects];
}

@end
